package com.dt.web.excel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.ListUtils;
import com.dt.system.domain.ModRepositories;
import com.dt.system.domain.ModRepositoriesLayer;
import com.dt.system.domain.ModRepositoriesShelves;
import com.dt.system.service.IModRepositoriesLayerService;
import com.dt.system.service.IModRepositoriesService;
import com.dt.system.service.IModRepositoriesShelvesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * 直接用map接收数据
 *
 * @author yxg
 */
@Component
public class RepositoriesLayerDataListener extends AnalysisEventListener<Map<Integer, String>> {

    private static IModRepositoriesShelvesService repositoriesShelvesService;

    private static IModRepositoriesLayerService repositoriesLayerService;


    @Autowired
    public void setService(IModRepositoriesShelvesService repositoriesShelvesService){
        RepositoriesLayerDataListener.repositoriesShelvesService=repositoriesShelvesService;
    }


    @Autowired
    public void setService(IModRepositoriesLayerService repositoriesLayerService){
        RepositoriesLayerDataListener.repositoriesLayerService=repositoriesLayerService;
    }



    /**
     * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 100;
    private List<Map<Integer, String>> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext context) {
        cachedDataList.add(data);
        if (cachedDataList.size() >= BATCH_COUNT) {
            saveData();
            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData();
        System.out.println("所有数据解析完成！");
    }

    /**
     * 加上存储数据库
     */
    private void saveData() {

        List<ModRepositoriesLayer> dataList=new ArrayList<>();
        //遍历所有行数
        for (Map<Integer,String> item: cachedDataList) {
            //创建对象
            ModRepositoriesLayer modRepositoriesLayer=new ModRepositoriesLayer();
            modRepositoriesLayer.setLayerNo(item.get(4));
            modRepositoriesLayer.setBoxNum(Long.parseLong(item.get(5)));
            modRepositoriesLayer.setBoxUseNum(Long.parseLong(item.get(6)));
            modRepositoriesLayer.setFileUseNum(Long.parseLong(item.get(7)));


            //查询该层格所属库架

            ModRepositoriesShelves modRepositoriesShelves=new ModRepositoriesShelves();
            modRepositoriesShelves.setShelvesNo(item.get(3));
            List<ModRepositoriesShelves> lst1=repositoriesShelvesService.selectModRepositoriesShelvesList(modRepositoriesShelves);
            if(!lst1.isEmpty())
            {
                modRepositoriesLayer.setShelvesId(lst1.get(0).getId());
                modRepositoriesLayer.setRepositoriesId(lst1.get(0).getRepositoriesId());
            }



            //查询数据是否存在，如果不存在，则新增，存在则忽略
            ModRepositoriesLayer temp=new ModRepositoriesLayer();
            temp.setLayerNo(item.get(4));
            List<ModRepositoriesLayer> rllst = repositoriesLayerService.selectModRepositoriesLayerList(temp);
            if(rllst==null || rllst.isEmpty())
            {
                dataList.add(modRepositoriesLayer);

            }

        }

        //批量添加
        if(!dataList.isEmpty()){
            repositoriesLayerService.batchInsertModRepositoriesLayer(dataList);
        }


    }
}